﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FxhjpgWeb.WebSite.Code;
using FxhjpgWeb.Model;
using FxhjpgWeb.Business;
using Ajeelee.Core.Utils;

namespace FxhjpgWeb.WebSite.WebAdmin
{
    public partial class CategoryEdit : AuthPageBase
    {
        protected int id;
        protected int pid;
        protected NewsClass info = new NewsClass();
        INewsClassBusiness newsClassBll = new NewsClassBusinessFactory().CreateNewsClassBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                id = QueryParamInt32("id");
                pid = QueryParamInt32("pid");
                if (id > 0)
                {
                    info = newsClassBll.GetById(id);
                    if (info == null)
                    {
                        info = new NewsClass();
                        MessageBoxGo("不存在该分类", "Category.aspx?pid=" + pid);
                    }
                    pid = info.PId;
                }

                if (IsPost)
                {
                    // 更新
                    if (id > 0)
                    {
                        initModel();
                        newsClassBll.Update(info);
                        Function.RemoveCacheObject("/cache/newsclassall/");
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("Category.aspx?pid=" + pid);
                    }
                    else // 新建
                    {
                        initModel();
                        newsClassBll.Insert(info);
                        System.Threading.Thread.Sleep(500);
                        Function.RemoveCacheObject("/cache/newsclassall/");
                        Page_GoTo("Category.aspx?pid=" + pid);
                    }
                }
            }
            catch (System.Threading.ThreadAbortException) { }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }
        public void initModel()
        {
            if (id <= 0)
            {
                info = new NewsClass();
            }
            if (info.Author.IsNullOrEmpty())
            {
                info.Author = User.Identity.Name;
                info.InIP = IPAddress;
            }
            info.Name = FormParam("mname");
            info.Name_En = FormParam("mname_en");
            info.PId = FormParamInt32("pid");
        }
    }
}